package org.hot100.h226;

/**
 * @Author: wjy
 * @Date: 2022/2/11 16:59
 */
public class Solution {

    public static void main(String[] args) {

        TreeNode treeNode = new TreeNode(1);
        TreeNode treeNode2 = new TreeNode(3);
        TreeNode treeNode3 = new TreeNode(2, treeNode, treeNode2);
        TreeNode treeNode4 = new TreeNode(6);
        TreeNode treeNode5 = new TreeNode(9);
        TreeNode treeNode6 = new TreeNode(7, treeNode4, treeNode5);
        TreeNode treeNode7 = new TreeNode(4, treeNode3, treeNode6);

        Solution solution = new Solution();

        solution.print(treeNode7);

        TreeNode invertTree = solution.invertTree(treeNode7);

        System.out.println("==========================================");

        solution.print(invertTree);

    }

    public void print(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.println(root.val);

        if (root.left != null) {
            this.print(root.left);
        }
        if (root.right != null) {
            this.print(root.right);
        }

    }

    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return root;
        }

        TreeNode temp = null;

        if (root.left != null) {
            root.left = this.invertTree(root.left);
        }
        if (root.right != null) {
            root.right = this.invertTree(root.right);
        }

        temp = root.left;
        root.left = root.right;
        root.right = temp;

        return root;
    }



}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}